﻿namespace PowerSong.SongDatabase
    {
    using System.Collections.Generic;
    using System.IO;

    public class Style : BaseEntity
        {

        public string Name { get; set; }

        public uint BackgroundColor { get; set; }

        public string BackgroundFileName { get; set; }

        public SubStyle[] SubStyles { get; private set; }

        public Style()
            : this(string.Empty)
            {
            }

        public Style(string name)
            : base(FileEntryType.Style, 1)
            {
            this.Name = name;
            this.BackgroundColor = 0;
            this.BackgroundFileName = string.Empty;
            this.SubStyles = new SubStyle[] { };
            }

        public SubStyle GetSubStyle(string name)
            {
            foreach (var subStyle in SubStyles)
                {
                if (subStyle.Name == name) return subStyle;
                }
            return null;
            }

        public void ClearSubStyles()
            {
            this.SubStyles = new SubStyle[] { };
            }

        public void AddSubStyle(SubStyle subStyle)
            {
            var list = new List<SubStyle>(SubStyles);
            list.Add(subStyle);
            this.SubStyles = list.ToArray();
            }

        internal override bool Deserialise(Database database, BinaryReader reader, int version)
            {

            // Ensure the version is correct
            if (version != 1) return false;

            // Get the details
            this.Name = reader.ReadString();
            this.BackgroundColor = reader.ReadUInt32();
            this.BackgroundFileName = reader.ReadString();

            // Get substyles
            var subStyleCount = reader.ReadInt32();
            var subStyles = new SubStyle[subStyleCount];
            for (var i = 0; i < subStyleCount; i++)
                {
                subStyles[i] = new SubStyle(reader);
                }
            this.SubStyles = subStyles;

            // Details read in successfully
            return true;

            }

        internal override void Serialise(BinaryWriter writer)
            {

            // Write general details
            writer.Write(Name);
            writer.Write(BackgroundColor);
            writer.Write(BackgroundFileName);

            // Write substyle details
            writer.Write(SubStyles.Length);
            foreach (var subStyle in SubStyles)
                {
                subStyle.WriteDetails(writer);
                }

            }

        public void CopyTo(Style otherStyle)
            {

            // Copy main details
            otherStyle.Name = this.Name;
            otherStyle.BackgroundColor = this.BackgroundColor;
            otherStyle.BackgroundFileName = this.BackgroundFileName;

            // Copy sub styles
            otherStyle.ClearSubStyles();
            foreach (var subStyle in SubStyles)
                {
                var copiedSubStyle = new SubStyle(subStyle.Name)
                    {
                    Name = subStyle.Name,
                    CrossFade = new SubStyleFadeDetails(subStyle.CrossFade.Type, subStyle.CrossFade.Duration),
                    FadeIn = new SubStyleFadeDetails(subStyle.FadeIn.Type, subStyle.FadeIn.Duration),
                    FadeOut = new SubStyleFadeDetails(subStyle.FadeOut.Type, subStyle.FadeOut.Duration),
                    FontColor = subStyle.FontColor,
                    FontEffect = subStyle.FontEffect,
                    FontEffectColor = subStyle.FontEffectColor,
                    FontName = subStyle.FontName,
                    FontSize = subStyle.FontSize,
                    FontStyle = subStyle.FontStyle,
                    Margin = new SubStyleMargin(subStyle.Margin.Left, subStyle.Margin.Top, subStyle.Margin.Right, subStyle.Margin.Bottom),
                    TextAlignment = subStyle.TextAlignment,
                    Visibility = subStyle.Visibility,
                    };
                otherStyle.AddSubStyle(copiedSubStyle);
                }
            }

        public override string ToString()
            {
            return Name;
            }

        }

    }
